Add additional HTTP verbs put, patch, and delete.

Andrew Cantino лет %!s(int64=10): %!d(string=назад)
Родитель
Сommit
edecac092e
2 измененных файлов с 40 добавлено и 19 удалено
  1. 11 3
      app/models/agents/post_agent.rb
  2. 29 16
      spec/models/agents/post_agent_spec.rb

+ 11 - 3
app/models/agents/post_agent.rb

@@ -9,6 +9,8 @@ module Agents
9 9
 
10 10
       The `post_url` field must specify where you would like to send requests. Please include the URI scheme (`http` or `https`).
11 11
 
12
+      The `method` used can be any of `get`, `post`, `put`, `patch`, and `delete`.
13
+
12 14
       The `headers` field is optional.  When present, it should be a hash of headers to send with the request.
13 15
     MD
14 16
 
@@ -76,7 +78,13 @@ module Agents
76 78
 
77 79
     def handle(data)
78 80
       if method == 'post'
79
-        post_data(data)
81
+        post_data(data, Net::HTTP::Post)
82
+      elsif method == 'put'
83
+        post_data(data, Net::HTTP::Put)
84
+      elsif method == 'delete'
85
+        post_data(data, Net::HTTP::Delete)
86
+      elsif method == 'patch'
87
+        post_data(data, Net::HTTP::Patch)
80 88
       elsif method == 'get'
81 89
         get_data(data)
82 90
       else
@@ -84,9 +92,9 @@ module Agents
84 92
       end
85 93
     end
86 94
 
87
-    def post_data(data)
95
+    def post_data(data, request_type = Net::HTTP::Post)
88 96
       uri = generate_uri
89
-      req = Net::HTTP::Post.new(uri.request_uri, headers)
97
+      req = request_type.new(uri.request_uri, headers)
90 98
       req.form_data = data
91 99
       Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) }
92 100
     end

+ 29 - 16
spec/models/agents/post_agent_spec.rb

@@ -25,11 +25,24 @@ describe Agents::PostAgent do
25 25
         'somekey' => 'value'
26 26
       }
27 27
     }
28
+    @requests = 0
29
+    @sent_requests = { Net::HTTP::Get => [], Net::HTTP::Post => [], Net::HTTP::Put => [], Net::HTTP::Delete => [], Net::HTTP::Patch => [] }
28 30
 
29
-    @sent_posts = []
30
-    @sent_gets = []
31
-    stub.any_instance_of(Agents::PostAgent).post_data { |data| @sent_posts << data }
32
-    stub.any_instance_of(Agents::PostAgent).get_data { |data| @sent_gets << data }
31
+    stub.any_instance_of(Agents::PostAgent).post_data { |data, type| @requests += 1; @sent_requests[type] << data }
32
+    stub.any_instance_of(Agents::PostAgent).get_data { |data| @requests += 1; @sent_requests[Net::HTTP::Get] << data }
33
+  end
34
+
35
+  describe "making requests" do
36
+    it "can make requests of each type" do
37
+      { 'get' => Net::HTTP::Get, 'put' => Net::HTTP::Put,
38
+        'post' => Net::HTTP::Post, 'patch' => Net::HTTP::Patch,
39
+        'delete' => Net::HTTP::Delete }.each.with_index do |(verb, type), index|
40
+        @checker.options['method'] = verb
41
+        @checker.check
42
+        @requests.should == index + 1
43
+        @sent_requests[type].length.should == 1
44
+      end
45
+    end
33 46
   end
34 47
 
35 48
   describe "#receive" do
@@ -45,11 +58,11 @@ describe Agents::PostAgent do
45 58
       lambda {
46 59
         lambda {
47 60
           @checker.receive([@event, event1])
48
-        }.should change { @sent_posts.length }.by(2)
49
-      }.should_not change { @sent_gets.length }
61
+        }.should change { @sent_requests[Net::HTTP::Post].length }.by(2)
62
+      }.should_not change { @sent_requests[Net::HTTP::Get].length }
50 63
 
51
-      @sent_posts[0].should == @event.payload.merge('default' => 'value')
52
-      @sent_posts[1].should == event1.payload
64
+      @sent_requests[Net::HTTP::Post][0].should == @event.payload.merge('default' => 'value')
65
+      @sent_requests[Net::HTTP::Post][1].should == event1.payload
53 66
     end
54 67
 
55 68
     it "can make GET requests" do
@@ -58,10 +71,10 @@ describe Agents::PostAgent do
58 71
       lambda {
59 72
         lambda {
60 73
           @checker.receive([@event])
61
-        }.should change { @sent_gets.length }.by(1)
62
-      }.should_not change { @sent_posts.length }
74
+        }.should change { @sent_requests[Net::HTTP::Get].length }.by(1)
75
+      }.should_not change { @sent_requests[Net::HTTP::Post].length }
63 76
 
64
-      @sent_gets[0].should == @event.payload.merge('default' => 'value')
77
+      @sent_requests[Net::HTTP::Get][0].should == @event.payload.merge('default' => 'value')
65 78
     end
66 79
   end
67 80
 
@@ -69,9 +82,9 @@ describe Agents::PostAgent do
69 82
     it "sends options['payload'] as a POST request" do
70 83
       lambda {
71 84
         @checker.check
72
-      }.should change { @sent_posts.length }.by(1)
85
+      }.should change { @sent_requests[Net::HTTP::Post].length }.by(1)
73 86
 
74
-      @sent_posts[0].should == @checker.options['payload']
87
+      @sent_requests[Net::HTTP::Post][0].should == @checker.options['payload']
75 88
     end
76 89
 
77 90
     it "sends options['payload'] as a GET request" do
@@ -79,10 +92,10 @@ describe Agents::PostAgent do
79 92
       lambda {
80 93
         lambda {
81 94
           @checker.check
82
-        }.should change { @sent_gets.length }.by(1)
83
-      }.should_not change { @sent_posts.length }
95
+        }.should change { @sent_requests[Net::HTTP::Get].length }.by(1)
96
+      }.should_not change { @sent_requests[Net::HTTP::Post].length }
84 97
 
85
-      @sent_gets[0].should == @checker.options['payload']
98
+      @sent_requests[Net::HTTP::Get][0].should == @checker.options['payload']
86 99
     end
87 100
   end
88 101